import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

//查询数据库
public class jdbcdemo06 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/text","root","123456");
            //3.获取数据库操作对象
            stmt = conn.createStatement();
            //4.执行sql
            //String sql = "select text_id as id,text_name from text_detail";
            String sql = "select text_id ,text_name from text_detail";
            //int executeUpdate(insert/delete/update)
            //ResultSet executeQuery(select)
            rs  = stmt.executeQuery(sql);//专门执行DQL语句的方法
            //5.处理查询结果集
            /*
            boolean flag1 = rs.next();
            //System.out.println(flag1);//true
            if (flag1){
                //getString()方法的特点:不管数据库中的数据类型是什么,都以String类型取出
                String text_id = rs.getString(1);//jdbc中所有下标都是从1开始，不是从1开始
                String text_name = rs.getString(2);// 1  2  为列
                System.out.println(text_id + "," + text_name);
            }
            flag1 = rs.next();
            if (flag1){
                //getString()方法的特点:不管数据库中的数据类型是什么,都以String类型取出
                String text_id = rs.getString(1);//jdbc中所有下标都是从1开始，不是从1开始
                String text_name = rs.getString(2);//1   2   为列
                System.out.println(text_id + "," + text_name);
            }
            */
            while (rs.next()){
                /*
                String text_id = rs.getString(1);//jdbc中所有下标都是从1开始，不是从1开始
                String text_name = rs.getString(2);//1   2   为列
                System.out.println(text_id + "," + text_name);
                */
                //不是以列的下标获取的，是以列的名字获取的
                //String text_id = rs.getString("id");//不是表的列名称 是查询结果集的列名称
                String text_id = rs.getString("text_id");//jdbc中所有下标都是从1开始，不是从1开始
                String text_name = rs.getString("text_name");//1   2   为列
                System.out.println(text_id + "," + text_name);
                //除了可以以String类型取出 还可以以特定类型取出
            }
        }catch (Exception e ){
            e.printStackTrace();
        }finally {
            //6.释放资源
            if (rs != null){
                try{
                    rs.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if (stmt != null){
                try{
                    stmt.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if (conn != null){
                try{
                    conn.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
